package com.service.lx;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.dao.luo.CusDaoM;
import com.dao.lx.ServeDAO;
import com.dao.lx.ServeDAOMybatis;
import com.dao.lx.UserDAO;
import com.entity.Customer;
import com.entity.Serve;
import com.entity.User;
import com.service.ServiceException;
import com.utils.Pager;

@Service
@Transactional(rollbackFor = Exception.class)
public class ServeService {
	@Autowired
	private UserDAO userDao;
	@Autowired
	private CusDaoM cusDaoM;
	@Autowired
	private ServeDAO serveDAO;
	@Autowired
	private ServeDAOMybatis serveDAOM;
	/**
	 * 添加/修改/逻辑删除
	 * @param serve
	 * @throws ServiceException
	 */
	public void addAndUpdateServe(Serve serve) throws ServiceException{
		try {
			if(serve.getCusno()!=null){
				Customer cus=new Customer();
				cus.setCusno(serve.getCusno());
				List<Customer> customer=cusDaoM.selectCus(cus);
				if(customer.size()!=0){
					serve.setCustomer(customer.get(0));
				}else{
					serve.setCustomer(null);
				}
			}
			Serve Serve2=serveDAO.findOne(serve.getServeId());
			if(Serve2==null){
				serve.setServeIfdelete(new BigDecimal(1));
			}
			if(serve.getUserId1()!=null){
				User user=userDao.findOne(serve.getUserId1());
				if(user!=null){
					serve.setUser(user);
				}else{
					serve.setUser(null);
				}
			}
			serve.setServeIntodate(new Date());
			serveDAO.save(serve);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param serveId
	 * @throws ServiceException
	 */
	public void deleteServe(Long serveId) throws ServiceException{
		try {
			serveDAO.delete(serveId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 逻辑删除
	 * @param serveId
	 */
	public void UpdateServeIfdelete(Long serveId) {
		Serve serve = serveDAO.findOne(serveId);
		serve.setServeIfdelete(new BigDecimal(0));
		addAndUpdateServe(serve);
	}
	
	/**
	 * 批量删除
	 * @param complainIds
	 */
	public void deletArrIfDelete(List<Long> serveIds) {
		if(serveIds != null) {
			for(Long serveId: serveIds) {
				Serve serve = serveDAO.findOne(serveId);
				serve.setServeIfdelete(new BigDecimal(0));
				addAndUpdateServe(serve);
			}
		}
	}
	
	/**
	 * 单条查询
	 * @param serveId
	 * @return
	 */
	public Serve findOne(Long serveId) {
		return serveDAO.findOne(serveId);
	}

	/**
	 * 快速查询：服务类型
	 * @param serveClassify
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Serve> findServeClassify(String serveClassify,Integer curpage,  Integer pagesize) {
		return serveDAO.queryByServeClassify(serveClassify, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：服务方式
	 * @param serveWay
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Serve> findServeWay(String serveWay,Integer curpage,  Integer pagesize) {
		return serveDAO.queryByServeWay(serveWay, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：状态
	 * @param serveState
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Serve> findServeState(String serveState,Integer curpage,  Integer pagesize) {
		return serveDAO.queryByServeState(serveState, new PageRequest(curpage-1, pagesize));
	}

	/**
	 * 快速查询：主题
	 * @param serveTopic
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Serve> findServeTopic(String serveTopic,Integer curpage,  Integer pagesize) {
		return serveDAO.queryByServeTopic("%"+serveTopic+"%", new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 查询所有
	 * @param pagesize 页大小
	 * @return
	 */
	public Page<Serve> findAllServes(Integer curpage, Integer pagesize){
		return serveDAO.findAllServeIfdelete(new PageRequest(curpage-1, pagesize));
	}
	
	/**
	 * 快速查询（全部数据，服务类型，服务方式，状态，主题）
	 * @param field
	 * @param pagesize
	 * @return
	 */
	public Page<Serve> findFastAllServes(String field, Integer curpage, Integer pagesize){
		String str;
		if(field.startsWith("serveClassify")){
			str=field.replace("serveClassify", "");
			return findServeClassify(str,curpage, pagesize);
		}else if(field.startsWith("serveWay")){
			str=field.replace("serveWay", "");
			return findServeWay(str,curpage, pagesize);
		}else if(field.startsWith("serveState")){
			str=field.replace("serveState", "");
			return findServeState(str,curpage, pagesize);
		}else{
			return findAllServes(curpage,pagesize);
		}
	}
	
	/**
	 * 高级查询
	 * @param map
	 * @return
	 * @throws ParseException 
	 */
	public Pager<Serve> findHighAllServes(Map map){
		Serve serve = (Serve) JSONObject.parseObject(map.get("list").toString(), Serve.class);
		Integer curpage=(Integer)map.get("curpage");
		Integer pagesize=(Integer)map.get("pagesize");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(serve.getServeDateStartAndStop()!=null){
    		try {
    			serve.setServeDateStart(sdf.parse((String) serve.getServeDateStartAndStop().get(0)));
    			serve.setServeDateStop(sdf.parse((String) serve.getServeDateStartAndStop().get(1)));
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
        }
		List<Serve> list=(List<Serve>) serveDAOM.findTrim(serve);
		Pager<Serve> pager=new Pager<Serve>();
		if(list.size()!=0){
			list.get(list.size()-1);
			Integer start=(curpage-1)*pagesize;
			Integer stop=curpage*pagesize;
			if(list.size()<stop){
				stop=list.size();
			}
			if(list.size()<start){
				start=stop-list.size()%pagesize;
			}
			pager.setList(list.subList(start, stop));
			pager.setTotal(list.size());
		}else{
			pager.setList(new ArrayList());
			pager.setTotal(0);
		}
		return pager;
	}

}
